package com.wyhgs.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyhgs.reggie.common.CustomException;
import com.wyhgs.reggie.dto.SetmealDto;
import com.wyhgs.reggie.entity.Dish;
import com.wyhgs.reggie.entity.Setmeal;
import com.wyhgs.reggie.entity.SetmealDish;
import com.wyhgs.reggie.mapper.DishMapper;
import com.wyhgs.reggie.mapper.SetmealMapper;
import com.wyhgs.reggie.service.SetmealDishService;
import com.wyhgs.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author 宛远鸿
 * @version 1.0
 */
@Slf4j
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    /**
     *  新增套餐,同时保存套餐和菜品的关联关系
     * @param setmealDto
     */
    @Transactional
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐的基本信息，操作setmeal,执行insert操作
        this.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        //保存套餐和菜品的关联信息，操作setmeal_dish,执行insert操作
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 删除套餐,以及关联数据
     * @param ids
     */
    @Transactional
    @Override
    public void removeWithDish(List<Long> ids) {
//select count(*) from setmeal where id in (1,2,3) and status = 1
        //查询套餐状态，确定是否可用删除
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.in(Setmeal::getId,ids);
        setmealLambdaQueryWrapper.eq(Setmeal::getStatus,1);
        long count = this.count(setmealLambdaQueryWrapper);
        //如果不能删除，抛出一个业务异常
        if (count > 0) {
            throw new CustomException("套餐正在售卖中，不能删除");
        }
        //如果可以删除，先删除套餐表中的数据---setmeal
        this.removeByIds(ids);//传入参数为表主键的话可以用removeByIds
        //delete from setmeal_dish where setmeal_id in (1,2,3)
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
        //删除关系表中的数据----setmeal_dish
        setmealDishService.remove(setmealDishLambdaQueryWrapper);
    }

    /**
     *
     * @param ids
     * @param w
     */
    @Override
    public void state(List<Long> ids, int w) {
        //先查询到对应ids的套餐信息list
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.in(Setmeal::getId,ids);
        List<Setmeal> list = this.list(setmealLambdaQueryWrapper);
        //判断当前所选套餐售卖状态是否一致
        setmealLambdaQueryWrapper.eq(Setmeal::getStatus,w);
        long count = this.count(setmealLambdaQueryWrapper);
        if (count > 0) {
            throw new CustomException("当前所选套餐中的售卖状态有一个或多个售卖状态与操作后状态一致，操作失败请检查");
        }
        //改变每一个detmeal的status
       list = list.stream().map((item)->{
            item.setStatus(w);
            return item;
        }).collect(Collectors.toList());
        //更新updateById setmeal表
this.updateBatchById(list);
    }
}
